﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace PageOffice6_Net_Core.Controllers.InsertPageBreak2
{
    public class InsertPageBreak2Controller : Controller
    {
        private readonly IWebHostEnvironment _webHostEnvironment;

        public InsertPageBreak2Controller(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }

        public IActionResult Word()
        {
            PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);

            //在文档末尾插入分页符，并且在新的页中创建新的数据区域插入另一篇文档
            PageOfficeNetCore.Word.WordDocumentWriter wordDocument = new PageOfficeNetCore.Word.WordDocumentWriter();
            PageOfficeNetCore.Word.DataRegionWriter mydr1 = wordDocument.CreateDataRegion("PO_first", PageOfficeNetCore.Word.DataRegionInsertType.After, "[end]");
            mydr1.SelectEnd();
            wordDocument.InsertPageBreak();//插入分页符

            PageOfficeNetCore.Word.DataRegionWriter mydr2 = wordDocument.CreateDataRegion("PO_second", PageOfficeNetCore.Word.DataRegionInsertType.After, "[end]");
            mydr2.Value = "[word]/InsertPageBreak2/doc/test2.doc[/word]";

            poCtrl.SetWriter(wordDocument);

            //打开Word文档
            poCtrl.WebOpen("doc/test1.doc", PageOfficeNetCore.OpenModeType.docNormalEdit, "tom");
            ViewBag.poCtrl = poCtrl.GetHtml();
            return View();
        }


        public async Task<ActionResult> SaveDoc()
        {
            PageOfficeNetCore.FileSaver fs = new PageOfficeNetCore.FileSaver(Request, Response);
            await fs.LoadAsync();
            string webRootPath = _webHostEnvironment.WebRootPath;
            fs.SaveToFile(webRootPath + "/InsertPageBreak2/doc/" + "test3.doc");
            fs.CustomSaveResult = "ok";
            return  fs.Close();
            
        }
    }
}